<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.region" placeholder="选择登入角色">
          <el-option label="管理员" value="管理员"></el-option>
          <el-option label="教师" value="教师"></el-option>
          <el-option label="学生" value="学生"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">登录</el-button>
        <el-button type="success" @click="register()">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        name: "",
        password: "",
        region: "",
      },
      userlist: [],
    };
  },
  mounted(){
    this.getList();
  },
  methods: {
    async getList() {
      let result = await this.$http.getUserList();
      this.userlist = result.data.list;
      // console.log(result.data.list);
    },
    onSubmit(){
      let a = 1;
      for(let user of this.userlist){
        a = a+1;
        if(user.userName==this.form.name&&user.password==this.form.password&&user.role==this.form.region)
        {
          alert("登陆成功！");
          this.$router.push('/Adminpage');
          break;
        }
        if(a==this.userlist.length){
          alert("用户名或密码不正确！");
        }
      }
    },
    register(){
      this.$router.push('/Register');
    }
  },
};
</script>